package com.example.sale.Controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.sale.Service.IFeedbackService;
import com.example.sale.Service.IUserDetailService;
import com.example.sale.True.Feedback;
import com.example.sale.True.ProductDetail;
import com.example.sale.True.User;
import com.example.sale.True.UserDetail;
import com.example.sale.utils.JwtUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@CrossOrigin
@RequestMapping("feedback")
@RestController
public class FeedbackController {
    @Autowired
    private IFeedbackService iFeedbackService;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    IUserDetailService iUserDetailService;
    @PostMapping("/post")
    public Map<String,Object> postProduct(@RequestBody Feedback feedback){
        Map<String,Object> result = new HashMap<>();
        Integer id = feedback.getUserid();
        System.out.println(id);
        String author = iUserDetailService.getByNameId(id);
        System.out.println(author);
        feedback.setAuthor(author);
        System.out.println(feedback);
        boolean flag = iFeedbackService.isFeedBackExists(id);
        if(flag==true){
            result.put("status","false");
            return result;
        }
        iFeedbackService.save(feedback);
        result.put("state","success");
        return result;
    }
    @GetMapping("/search")
//    @Cacheable(value = "FeedBackData", key = "#pageNO + ':' + #pageSize + ':' + #time + ':' + #title+ ':' + #author")
//    @Cacheable(value = "FeedBackData", key = "#pageNO + ':' + #author + ':' + #title + ':' + #tiem + ':' + #pageSize")
    public Map<String,Object>SearchData(
            @RequestParam(value ="title",required = false)String title,
            @RequestParam(value ="time",required = false)String time,
            @RequestParam(value ="author",required = false)String author,
            @RequestParam(value = "pageNo") Integer pageNO,
            @RequestParam(value = "pageSize") Integer pageSize)
    {
        System.out.println("title is"+title);
        System.out.println("time is"+ time);
        System.out.println("author is"+ author);
        System.out.println("pageNo is"+pageNO);
        System.out.println("pageSize is "+pageSize);
        LambdaQueryWrapper<Feedback> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(title),Feedback::getTitle,title);
        wrapper.eq(StringUtils.hasLength(author),Feedback::getAuthor,author);
        wrapper.like(StringUtils.hasLength(time),Feedback::getTime,time);
        Page<Feedback> page =new Page<>(pageNO,pageSize);
        Map<String,Object> result = new HashMap<>();
        iFeedbackService.page(page,wrapper);
        result.put("status",page.getTotal());
        result.put("data",page.getRecords());
        return result;
    }
    @PutMapping("/Change")
    public Map<String, Object> changeData(@RequestBody Feedback feedback){
        System.out.println(feedback);
        Map<String, Object> response = new HashMap<>();
        response.put("status", iFeedbackService.updateById(feedback));
        return response;
    }
    @DeleteMapping("/{id}")
    public Map<String, Object> deleteData(@PathVariable Integer id){
        Map<String,Object> result = new HashMap<>();
        result.put("id",iFeedbackService.removeById(id));
        return result;
    }
    @GetMapping("/{id}")
    public Map<String,Object> getById(@PathVariable Integer id){
        System.out.println(id);
        Map<String,Object> result = new HashMap<>();
        result.put("data",iFeedbackService.getById(id));
        return result;
    }
    @GetMapping("selfData")
    public Map<String,Object> getBySelfID(HttpServletRequest request){
        String token = request.getHeader("token");
        User zhi = jwtUtil.parseToken(token, User.class);
        Integer userid = zhi.getId(); // 假设id是String类型;
        Feedback feedback = iFeedbackService.getFeedbackById(userid);
        Map<String,Object> result = new HashMap<>();
        result.put("data",feedback);
        return result;
    }
    @DeleteMapping("selfFeedback")
    public Map<String,Object> DeleteBySelfID(HttpServletRequest request){
        String token = request.getHeader("token");
        System.out.println(token);
        User zhi = jwtUtil.parseToken(token, User.class);
        Integer userid = zhi.getId(); // 假设id是String类型;
        boolean flag = iFeedbackService.removeFeedbackById(userid);
        System.out.println(flag);
        Map<String,Object> result = new HashMap<>();
        result.put("data",flag);
        return result;
    }
    @GetMapping("FeedBackData")
    public List<Integer> FeedBackData()
    {
        String ReSolvedNumber ="1";
        String NotReSolvedNumber ="0";
        List<Integer> dataList = new ArrayList<>();
        dataList.add(iFeedbackService.FeedbackNumber(NotReSolvedNumber));
        dataList.add(iFeedbackService.FeedbackNumber(ReSolvedNumber));
        dataList.add(iFeedbackService.FeedbackNumber(NotReSolvedNumber)+ iFeedbackService.FeedbackNumber(ReSolvedNumber));
        return dataList;
    }
}
